package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author : jiang
 * @date : 2023/10/13 20:21
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 修改分类
     * @param categoryDTO
     */
    @Override
    public void updateCategory(CategoryDTO categoryDTO) {
        //创建分类对象
        Category category = new Category();
        //拷贝数据
        BeanUtils.copyProperties(categoryDTO,category);
        //操作数据库
        categoryMapper.updateCategory(category);
    }

    /**
     * 分类分页查询
     * @param cpDTO
     */
    @Override
    public PageResult page(CategoryPageQueryDTO cpDTO) {
        //设置分页参数
        PageHelper.startPage(cpDTO.getPage(),cpDTO.getPageSize());
        Page<Category> page = categoryMapper.page(cpDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 修改分类状态
     * @param status
     * @param id
     */
    @Override
    public void updateStatus(Integer status,Long id) {
        Category category = Category.builder()
                .status(status)
                .id(id)
                .build();
        categoryMapper.updateCategory(category);
    }

    /**
     * 根据id删除分类
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        //删除前需要进行判断
        //如果该分类下有菜品 不能删除
        Integer count1 = dishMapper.getByCategoryId(id);
        Integer count2 = setmealMapper.getByCategoryId(id);
        if (count1 > 0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        if (count2 > 0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        //删除分类
        categoryMapper.deleteById(id);
    }

    /**
     * 新增分类
     * @param categoryDTO
     */
    @Override
    public void addCategory(CategoryDTO categoryDTO) {
        //创建分类对象
        Category category = new Category();
        //拷贝数据
        BeanUtils.copyProperties(categoryDTO,category);
        //补充数据
        //状态
        category.setStatus(StatusConstant.DISABLE);
        //操作数据库
        categoryMapper.addCategory(category);
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @Override
    public List<Category> getCategoryByType(Integer type) {
        return categoryMapper.getCategoryByType(type);
    }
}
